import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import viteSentry from 'vite-plugin-sentry';
import { generateReleaseVersion } from './common';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());

  const isProd = process.env.NODE_ENV === 'production';

  console.log('isProd', isProd);

  const sentryConfig = {
    url: 'http://172.22.82.13:9000',
    authToken: env.VITE_SENTRY_AUTH_TOKEN,
    org: 'sentry',
    project: 'project2',
    // configFile: './.sentryclirc',
    // cleanSourcemapsAfterUpload: true,
    release: generateReleaseVersion(),
    deploy: {
      env: 'production',
    },
    // 提交 git
    setCommits: {
      auto: true,
    },
    // debug: true,
    // finalize: true,
    sourceMaps: {
      dist: '1',
      include: ['./dist/assets'],
      ignore: ['node_modules'],
      // urlPrefix: 'http://172.22.82.13:9091/assets',
      // urlPrefix: '~/assets/',
      urlPrefix: '~/',
    },
  };

  return {
    build: {
      sourcemap: isProd,
    },
    plugins: [
      vue(), // Put the Sentry vite plugin after all other plugins
      isProd ? viteSentry(sentryConfig) : null,
    ],
  };
});
